package com.caipin.task;

import com.caipin.domain.table.CourseCurrent;
import com.caipin.domain.table.CourseTable;
import com.caipin.domain.table.CourseTimetable;
import com.caipin.domain.table.TeacherWork;
import com.caipin.service.CourseManagementService;
import com.caipin.service.StudentManagementService;
import com.caipin.service.TeacherManagementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: zhunian0322@163.com
 * @DateTime: 2020-08-04 9:33
 * @Description: TODO
 */
@Slf4j
@Component
public class MySchedulerTask {

    @Autowired
    private CourseManagementService courseManagementService;


    @Autowired
    private TeacherManagementService teacherManagementService;

    @Autowired
    private StudentManagementService studentManagementService;

    @Scheduled(cron = "0 0 0 * * *")
    public void scheduled() {
        log.info(new Date().toString());

        int week = getWeekByCurrentTime();

        List<CourseTable> courseTableList = courseManagementService.findCourseTableListByWeek(week);
        for (CourseTable courseTable : courseTableList) {
            List<TeacherWork> teacherWorkList = teacherManagementService.findTeacherWorkByCourse(
                    courseTable.getCourseName(), courseTable.getGrade(),
                    courseTable.getWeek(), courseTable.getSections());

            if (teacherWorkList == null || teacherWorkList.size() <= 0) {
                continue;
            }

            int count = 0;
            if (hasDigit(courseTable.getCourseName())){
                count = studentManagementService.studentCourseCount(courseTable.getCourseName(), courseTable.getGrade(), courseTable.getClbum(),
                        courseTable.getWeek(), courseTable.getSections());
            }else {
                count = studentManagementService.studentCourseCountByClassStudent(courseTable.getGrade(), courseTable.getClbum());
            }


            CourseTimetable timetable = courseManagementService.findCourseTimetableBySections(courseTable.getSections());

            for (TeacherWork teacherWork : teacherWorkList) {
                CourseCurrent courseCurrent = new CourseCurrent()
                        .setTeacherId(teacherWork.getTeacherId())
                        .setStudentNumber(count)
                        .setCourseName(courseTable.getCourseName())
                        .setBuilding(teacherWork.getBuilding())
                        .setClassroom(teacherWork.getClassroom())
                        .setDate(new Date())
                        .setSections(courseTable.getSections())
                        .setStartTime(timetable.getStartTime())
                        .setEndTime(timetable.getEndTime())
                        .setWeek(courseTable.getWeek())
                        .setGrade(courseTable.getGrade());

                courseManagementService.addCourseCurrent(courseCurrent);
            }

        }


    }

    private boolean hasDigit(String content) {
        boolean flag = false;
        Pattern p = Pattern.compile(".*\\d+.*");
        Matcher m = p.matcher(content);
        if (m.matches()) {
            flag = true;
        }
        return flag;
    }

    /**
     * @return 1-7
     */
    private int getWeekByCurrentTime() {
        Date date = new Date(System.currentTimeMillis());

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int i = cal.get(Calendar.DAY_OF_WEEK) - 1;
        return i;
    }
}
